package com.abu.crms.controller;


import cn.hutool.core.util.ObjectUtil;
import com.abu.crms.config.common.Resp;
import com.abu.crms.pojo.Classroom;
import com.abu.crms.pojo.Course;
import com.abu.crms.service.CourseService;
import com.abu.crms.utils.PageUtils;
import com.abu.crms.vo.QueryConditionVO;
import com.abu.crms.vo.QueryCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2021-02-03
 */
@RestController
@RequestMapping("/crms/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("getCourseListByCondition")
    public Resp getClassroomListByCondition(QueryCourseVo queryCourseVo) {
        HashMap<String, Object> resultHashMap = this.courseService.queryPageByCondition(queryCourseVo);
        return Resp.ok().data(resultHashMap);
    }

    @GetMapping("getCourseById/{id}")
    public Resp getCourseById(@PathVariable("id") Long id) {
        Course course = this.courseService.getCourseById(id);
        if (ObjectUtil.isNotNull(course)) {
            return Resp.ok().data("course", course);
        }
        return Resp.error().msg("教室资源不存在");
    }

    @GetMapping("checkCourseIsAvailable")
    public Resp checkDataIsAvailable(
            @RequestParam("classroomId") Long id,
            @RequestParam("beginTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime beginTime,
            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime endTime
    ) {
        Boolean flag = this.courseService.checkData(id, beginTime, endTime);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("时间有冲突，请更换教室或时间段");
    }

    @PostMapping("addCourse")
    public Resp addCourse(@RequestBody Course course) {
        Boolean flag = this.courseService.saveCourse(course);
        if (flag) {
            return Resp.ok().msg("添加成功");
        }
        return Resp.error().msg("添加失败");
    }


    @PutMapping("updateCourse")
    public Resp modifiedClassroom(@RequestBody Course course) {
        Boolean flag = this.courseService.modifiedClassroom(course);
        if (flag) {
            return Resp.ok().msg("修改成功");
        }
        return Resp.error().msg("修改失败");
    }

    @DeleteMapping("deleteCourse/{id}")
    public Resp deleteCourse(@PathVariable("id") Long id) {
        boolean flag = this.courseService.deleteCourse(id);
        if (flag) {
            return Resp.ok();
        }
        return Resp.error().msg("删除成功");
    }

}

